package com.molyfun.parents.activity.course.coursehistory;

import android.content.Context;
import android.text.TextUtils;

import com.molyfun.parents.utils.PreferencesStorageUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:01/02/2018.
 */

public class CourseSearchHistoryManager {

    public static List<String> getSearchHistory(Context context){
        if(context == null){
            return null;
        }
        String tagListStr = PreferencesStorageUtil.getCourseSearchHistory(context);
        if(TextUtils.isEmpty(tagListStr)){
            return null;
        }else{
            return new ArrayList<>(Arrays.asList(tagListStr.split(",")));
        }

    }

    public static void clearSearchHistory(Context context){
        PreferencesStorageUtil.setCourseSearchHistory(context , "");
    }

    public static void setSearchHistory(Context context, List<String> list){
        if(list != null && list.size() > 0){
            StringBuilder sb = new StringBuilder();
            for(String str : list){
                sb.append(str);
                sb.append(",");
            }
            sb.deleteCharAt(sb.length() - 1);
            PreferencesStorageUtil.setCourseSearchHistory(context, sb.toString());
        }else{
            clearSearchHistory(context);
        }
    }

}
